function cleanDiv(divId){
	document.getElementById(divId).innerHTML='<p></p>';
}
function getTarget(str,target_id,elementId,servletName){
	var xmlhttp;
	var method="";
	var action="";
	
	
	
	if (str.length==0){
		document.getElementById("target").innerHTML="<p>Errore</p>";
		return;
	}
	else{
				
		var dotPosition= str.lastIndexOf(".");
		
		action=str.substring(0,dotPosition);
		method = str.substring(dotPosition+1,str.length);
//		alert(servletName);
		
		
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			
			var response= xmlhttp.responseXML;
//			
			var responseText= xmlhttp.responseText;
			
			
			var returnMessage=document.getElementById(target_id);
			
			
			

				var responseCode = response.getElementsByTagName('ResponseCode')[0].firstChild.nodeValue	;

				if(responseCode=='OK'){
					
					var responseHTML = document.createElement('response');
					responseHTML.innerHTML = responseText;
					var elements = responseHTML.getElementsByTagName('div');
					
					if(elements.length>0){
						returnMessage.innerHTML=elements[0].innerHTML;
					}
					else{
						returnMessage.innerHTML=response.getElementsByTagName('ResponseMessage')[0].firstChild.nodeValue;
					}
					
					
					
				
				}
				else{
					returnMessage.innerHTML=response.getElementsByTagName('ResponseMessage')[0].firstChild.nodeValue;
				}
//				
//			}else{
//				document.getElementById(target_id).innerHTML=xmlhttp.responseText;
//			}
//			
//			
			
			
		}
	};
	
	var url="";
	var values="";
	
	var checkBoxValues="";
	if(method=='find'){
//		var findSpan=document.getElementById('findFilters');
		
		
		
		var inputFields= document.getElementsByTagName('input');
		 
//		var nodeList=findSpan.childNodes; 
		
		 for (var i = 0; i < inputFields.length; i++) {
		     var node = inputFields[i];
		  
		     if (node.getAttribute('type') == 'input' || node.getAttribute('type') == 'hidden') {
		    	if(node.value!=''){
		    		values+="&"+node.name+"="+node.value;
		    	}
		     }
		 } 
		 

		var inputFields=document.getElementsByTagName("input");
			
		for(i=0;i<inputFields.length;i++){
//					alert(inputFields[i].value);
			if(inputFields[i].value!='' && inputFields[i].type!='checkbox'){
				values+="&"+inputFields[i].name+"="+inputFields[i].value;
			}
			
		}
				
		var textAreaFields=document.getElementsByTagName("textarea");
		
		for(i=0;i<textAreaFields.length;i++){
//			alert(inputFields[i].value);
			if(textAreaFields[i].value!='' && textAreaFields[i].type!='checkbox'){
				values+="&"+textAreaFields[i].name+"="+textAreaFields[i].value;
			}
			
		}		
				
				
		var checkBoxes=getElementByType("checkbox");
		if(checkBoxes.length>0){
			
			var checkboxName=checkBoxes[0].name;
			
//					alert("name :"+checkBoxes.length);
			
			checkBoxValues += checkBoxes[0].value;
//					alert("checkBoxValues :"+checkBoxValues);
			for(i=0;i<checkBoxes.length;i++){
				if(checkboxName==checkBoxes[i].name ){
					
					if(checkBoxes[i].checked==true && checkBoxes[i].value!=checkBoxValues){
						if(checkBoxValues==""){
							checkBoxValues+=checkBoxes[i].value;
						}
						else{
							checkBoxValues += "|-|"+checkBoxes[i].value  ;
						}
						
					}
					
					
				}else{
//							alert(checkBoxValues);
					
					values+="&"+checkboxName+"="+checkBoxValues;
					checkBoxValues ="";
					checkboxName=checkBoxes[i].name;
					if(checkBoxes[i].checked==true){
						checkBoxValues += checkBoxes[i].value;
					}
					
					
				}
				if(i==checkBoxes.length-1){
					values+="&"+checkboxName+"="+checkBoxValues;
				}
			}
		}
	}
	if(method=='save' || method=='delete' ){
		var inputFields=document.getElementsByTagName("input");
			
			for(i=0;i<inputFields.length;i++){
//				alert(inputFields[i].value);
				if(inputFields[i].value!='' && inputFields[i].type!='checkbox'){
					values+="&"+inputFields[i].name+"="+inputFields[i].value;
				}
				
			}
			
			var textAreaFields=document.getElementsByTagName("textarea");
			
			for(i=0;i<textAreaFields.length;i++){
//				alert(inputFields[i].value);
				if(textAreaFields[i].value!='' && textAreaFields[i].type!='checkbox'){
					values+="&"+textAreaFields[i].name+"="+textAreaFields[i].value;
				}
				
			}
			
			
			
			var checkBoxes=getElementByType("checkbox");
			if(checkBoxes.length>0){
				
				var checkboxName=checkBoxes[0].name;
				
//				alert("name :"+checkBoxes.length);
				
				checkBoxValues += checkBoxes[0].value;
//				alert("checkBoxValues :"+checkBoxValues);
				for(i=0;i<checkBoxes.length;i++){
					if(checkboxName==checkBoxes[i].name ){
						
						if(checkBoxes[i].checked==true && checkBoxes[i].value!=checkBoxValues){
							if(checkBoxValues==""){
								checkBoxValues+=checkBoxes[i].value;
							}
							else{
								checkBoxValues += "|-|"+checkBoxes[i].value  ;
							}
							
						}
						
						
					}else{
//						alert(checkBoxValues);
						
						values+="&"+checkboxName+"="+checkBoxValues;
						checkBoxValues ="";
						checkboxName=checkBoxes[i].name;
						if(checkBoxes[i].checked==true){
							checkBoxValues += checkBoxes[i].value;
						}
						
						
					}
					if(i==checkBoxes.length-1){
						values+="&"+checkboxName+"="+checkBoxValues;
					}
				}
			}
			
	}
	document.getElementById(target_id).innerHTML="<p></p>";
	
//	document.getElementById('loaderImage').style.display="block";
	
	
	if(elementId!=''){
		values+="&id="+elementId;
	}
	values+="&target="+target_id;
	if(method=='delete'){
		if(!confirm("Cancellare l'elemento con id: "+elementId)){
			return;
		}
	}
	url="/gestione_candidature/"+servletName+"?action="+action+"&method="+method+values;
	
//	alert(url);
	
	
	// inizializza campi date
	
	init('candidatoListTable',400);
	init('formTable',400);
	init('formPanelTable',400);
	
	
	
	xmlhttp.open("GET",url,true);
	
	xmlhttp.send();
	
}
function callBackTest(){
	alert('callBacktest');
}
function init(target,time) {
	(function () {
        var a = document.getElementById(target) ;
		
        if (a) {
//        	alert('test');
        	calendar.set('dataNascita');
        	calendar.set('dataColloquio');
        	calendar.set('scadenzaContratto');
        	calendar.set('dateStart1');
        	calendar.set('dateStart2');
//        	document.getElementById('loaderImage').style.display="none";
        	
        }
        else {
            setTimeout(arguments.callee, time); // call myself again in  msecs
//            document.getElementById('loaderImage').style.display="none";
        }
        
    }());
	
	} 


function setSelected(menuId,menuIndex){
	var menu=document.getElementById(menuId);
	var liElements=menu.childNodes[0].childNodes;
	for ( var i = 0; i < liElements.length; i++) {
		if (menuIndex==i) {
			liElements[menuIndex].setAttribute("class","selected");
			return;
		}else{
			liElements[i].setAttribute("class","");
		}
	}
	
//	alert('ok');
	
}
function test(){
	alert('ok');
}
function savePlayer(){
//	alert('ok');
	setDefaultInputStyle('left_input');
	clearHeaderMessage();
	if (checkEmptyFields()) {
		return;
	}
	if (checkWrongUserPassword()) {
		return;
	}
	if (checkEmail()) {
		return;
	}
	
}
function checkEmptyFields(){
	var inputFields=document.getElementsByTagName("input");
	for ( var i = 0; i < inputFields.length; i++) {
		if (inputFields[i].value=='') {
			inputFields[i].setAttribute("class","input_focused");
			inputFields[i].focus();
			
			var header_message=document.getElementById("header_message");
			var input_label=document.getElementsByTagName("label")[i];
			header_message.innerHTML="Valorizzare il campo "+input_label.innerHTML;
			
			return true;
		}
	}
	return false;
}
function checkWrongUserPassword() {
	var password=document.getElementById("password");
	var confirmed_pass=document.getElementById("confirm_pass");
	
	if (password.value!=confirmed_pass.value) {
		password.value='';
		confirmed_pass.value='';
		
		password.setAttribute("class","input_focused");
		password.focus();
		
		var header_message=document.getElementById("header_message");
		header_message.innerHTML="il Campo 'Password' e 'Conferma Password' non corrispondono. Riprovare";
		
		return true;
	}
	return false;
}
function checkEmail(){
	var emailField=document.getElementById("email");
	if (!IsEmail(emailField)) {
		
		emailField.setAttribute("class","input_focused");
		emailField.focus();
		
		var header_message=document.getElementById("header_message");
		header_message.innerHTML="il Campo 'Email' non risulta conforme.";
		return true;
	}
	return false;
}
function setDefaultInputStyle(className){
	var inputFields=document.getElementsByTagName("input");
	for ( var i = 0; i < inputFields.length; i++) {
		if (inputFields[i].type!='button') {
			inputFields[i].setAttribute("class",className);
		}
	}
}
function IsEmail(elem) {
    var field = elem.value;
    var regex = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
//    alert(field);
    return regex.test(field);
}
function clearHeaderMessage(){
	var header_message=document.getElementById("header_message");
	header_message.innerHTML="";
}

function target_popup(form_id) {
	
//    window.open('', 'formpopup', 'width=400,height=400,resizeable,scrollbars');
//    document.getElementById(form_id).setAttribute("style","display:inline;");
//    document.getElementById('cv_load').setAttribute("style","display:none;");
	
	var form= document.getElementById(form_id);
	
	var skill= document.getElementById('select_skill');
	
//	alert('skill '+skill.value );
	
	var newWindow = window.open('./pages/uploadFile.jsp?skill='+skill.value, '', 'height=200,width=250');
	var c=0;

	
	
	
}

function setHiddenField(fieldName,value){
//	alert("setHiddenField");
	
	var element= document.getElementById(fieldName);
	element.setAttribute("value",value);
}

function getElementByType(type){
	 var node_list = document.getElementsByTagName('input');
	 
	 var nodes= new Array();
	 
	 
	 for (var i = 0; i < node_list.length; i++) {
	     var node = node_list[i];
	  
	     if (node.getAttribute('type') == type) {
	    	 nodes.push(node);
	     }
	 } 

	 return nodes;
}

function filterPanel(){
	
	var filterPanel=document.getElementById('findFilters');
	
	if(filterPanel.style.display== 'none'){
		filterPanel.style.display= 'block';
	}else{
		filterPanel.style.display= 'none';
	}
}
function checkFileSearch(){
//	alert('checkFileSearch');
	var ricercaSuFile=document.getElementById('ricercaSuFile');
	
	var textToSearch=document.getElementById('textToSearch');
	
	if(ricercaSuFile.checked==true){
		textToSearch.disabled=false;
		ricercaSuFile.value="1";
	}else{
		textToSearch.disabled=true;	
		textToSearch.value="";
		ricercaSuFile.value="0";
	}
	
}
function checkDateRange(start, end) {
		if(start!='' && end!=''){
		 // Parse the entries
		   var startDate = Date.parse(start);
		   var endDate = Date.parse(end);
		   // Make sure they are valid
		    if (isNaN(startDate)) {
		      alert("The start date provided is not valid, please enter a valid date.");
		      return false;
		   }
		   if (isNaN(endDate)) {
		       alert("The end date provided is not valid, please enter a valid date.");
		       return false;
		   }
		   // Check the date range, 86400000 is the number of milliseconds in one day
		   var difference = (endDate - startDate) ;// (86400000 * 7);
		   if (difference < 0) {
		       alert("The start date must come before the end date.");
		       return false;
		   }
//			   if (difference <= 1) {
//			       alert("The range must be at least seven days apart.");
//			       return false;
//			    }
		}
	  
	   return true;
}

function getCheckedTarget(field1,field2,str,target_id,elementId,servletName){
	if(checkDateRange(document.getElementById(field1).value, document.getElementById(field2).value)){
		getTarget(str,target_id,elementId,servletName);
	}
}
function showPopup(id){
	var popup=document.getElementById('popup_box_'+id);
	var showpopupButton=document.getElementById('showpopup_'+id);
	
	
	var nodes= new Array();
	nodes=getElementByClass("showpopup");
//	alert(nodes.length);
//	
	for(i=0;i<nodes.length;i++){
		nodes[i].style.opacity="0.4";
//		alert(nodes[i].id);
	}
	
	popup.style.display="inline";
	showpopupButton.style.display="none";
	
	var rowID=document.getElementById('row_'+id);
	
	var rectObject= rowID.parentNode.getBoundingClientRect();
	
	popup.style.position="absolute";
	popup.style.top=rectObject.top+"px";
//	popup.style.left=rectObject.left+"px";
	
	popup.style.left="91%";
	popup.style.width="90px";

	
}
function hidePopup(id){
	var popup=document.getElementById('popup_box_'+id);
	var showpopupButton=document.getElementById('showpopup_'+id);

	popup.style.display="none";
	showpopupButton.style.display="inline";
	
	nodes=getElementByClass("showpopup");

	for(i=0;i<nodes.length;i++){
		nodes[i].style.opacity="1";

	}
	
}
function getElementByClass(className){
	var node_list = document.getElementsByTagName('*');
	 
	 var nodes= new Array();
	 
	 
	 for (var i = 0; i < node_list.length; i++) {
	     var node = node_list[i];
	  
	     if (node.className == className) {
	    	 nodes.push(node);
	     }
	 } 
	return nodes
}

function handlePanel(str,target_id,elementId,servletName){
	var panelTarget=document.getElementById('panel_target');
	
	var showPanelLabel=document.getElementById('showPanelBox_label');
	var hidePanelLabel=document.getElementById('hidePanelBox_label');
	
	if(panelTarget.style.display==''){
		showPanelLabel.style.display='none';
		hidePanelLabel.style.display='block';
		
		panelTarget.style.display='block';
		
		
		getTarget(str,target_id,elementId,servletName);
	}else{
		showPanelLabel.style.display='block';
		hidePanelLabel.style.display='none';
		
		panelTarget.style.display='';
//		panelTarget.innerHtml='';
	}
	
	
	
	
}